namespace IQIGame.Onigao.Game
{
    using ResLoader = IQIGame.Onigao.Framework.ResLoader;

    /// <summary>
    /// 用来创建UI的配置
    /// </summary>
    public interface IUICreateConfig
    {
        /// <summary>
        /// UI prefab的资源名
        /// </summary>
        public string prefabName { get; }
        /// <summary>
        /// prefab的父级路径(默认不需要)
        /// </summary>
        public string parentPath { get; }
        /// <summary>
        /// UI所在的层级
        /// </summary>
        public EUILayer layer { get; }
        /// <summary>
        /// 该UI是否是常驻的，不会被UIManager普通的DeleteAll删除掉
        /// </summary>
        public bool permanent { get; }

        /// <summary>
        /// 是否是全屏界面
        /// </summary>
        public bool isFullScreen { get; }

        /// <summary>
        /// 激活模糊背景
        /// blurDownSampling默认值改为2
        /// 由于不希望写UI的初级程序关心默认降采样，所以专门开一个activeBlurBg属性来判断是否开启模糊UI，而不是直接通过blurDownSampling是否大于0
        /// </summary>

        public bool activeBlurBg { get; }

        /// <summary>
        /// UI模糊配置，rt的降采样尺寸 ，0 表示不采样
        /// </summary>
        public int blurDownSampling { get; }

        /// <summary>
        /// UI的实际路径
        /// </summary>
        public string prefabPath { get; }

        public IUICreateConfig Init();
    }

    public class UICreateConfig : IUICreateConfig
    {
        public string prefabName { get; set; }
        public string parentPath { get; set; } = string.Empty;
        public EUILayer layer { get; set; }
        public bool permanent { get; set; }
        public bool isFullScreen { get; set; }
        public bool activeBlurBg { get; set; }
        public int blurDownSampling { get; set; } = 2;
        public string prefabPath { get; private set; }

        public IUICreateConfig Init()
        {
            if (string.IsNullOrEmpty(prefabPath))
            {
                string _fp = this.prefabName;
                if (!string.IsNullOrEmpty(this.parentPath))
                {
                    _fp = this.parentPath + "/" + _fp;
                }
                this.prefabPath = FrameworkPath.GetUIPrefabPath(_fp);
            }
            return this;
        }
    }

    /// <summary>
    /// 一些需要用的数据
    /// </summary>
    public class UIWindowSetting
    {
        public IUICreateConfig cfgRuntime { get; private set; }
        public EUILayer layer => this.cfgRuntime.layer;
        public bool permanent => this.isInited && this.cfgRuntime.permanent;
        public bool isFullScreen => this.isInited && this.cfgRuntime.isFullScreen;

        public string sortLayerName { get; private set; }
        public int sortOrder { get; internal set; }
        public ResLoader resloader { get; internal set; }

        public bool isInited { get; private set; }

        public UIWindowSetting Init(IUICreateConfig cfg, int sortOrder, ResLoader loader)
        {
            this.cfgRuntime = cfg;
            this.sortOrder = sortOrder;
            this.resloader = loader;
            this.sortLayerName = layer.ToString();
            this.isInited = true;
            return this;
        }

        public void Clear()
        {
            this.isInited = false;
        }
    }
}
